<?php

namespace app\admin\controller;


use app\admin\model\ResultModel;
use think\Db;

class Result extends Base
{

    /**
     * [index 轮播列表]
     * @return [type] [description]
     * @author felix
     */
    public function index(){

        $key = input('key');  //名称
        $question_id= input('question_id');  //名称
        $map = [];
        //$map['is_show'] = 1;
        if($key&&$key!==""){
            $map['title'] = ['like',"%" . $key . "%"];
        }
        if($question_id && $question_id!==""){
            $map['question_id'] = $question_id;
        }
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = config('list_rows');// 获取总条数
        $count = Db::name('result')->where($map)->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $goods_cat = new ResultModel();
        //dump($map);
        $lists = $goods_cat->getDataByWhere($map, $Nowpage, $limits);
        //dump($lists);
        $type = ['1'=>'总分', '2'=>'维度分析'];
        // dump($lists);
        foreach($lists as $k=>$v)
        {
            $lists[$k]['echarts_type_name'] = $type[$v['echarts_type']]??'';
            $o_names = '';
            if($v['result_ids']){
                $option_ids = explode(',',$v['result_ids']);
                $n = Db::name('option_result')->whereIn('id',$option_ids)->column('name');
                $o_names = implode(',',$n);
                $o_names = trim($o_names,',');
            }
            $lists[$k]['o_names'] = $o_names;
            //$lists[$k]['is_show'] = $v['is_show'] == 1 ? '显示':'隐藏' ;

        }
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('val', $key);
        $this->assign('question_id', $question_id);

        $option = model('OptionResultModel')->order('sort asc')->field('id,name')->select();
        $this->assign('option',$option);

        $question = model('QuestionModel')->order('sort asc')->field('id,name')->select();
        $this->assign('question',$question);

        if(input('get.page'))
        {
            return json($lists);
        }
        return $this->fetch();
    }




    /**
     * @return [type] [description]
     * @author felix
     */
    public function resultAdd()
    {
        //$key = input('result_id');
        if(request()->isAjax()){

            $param = input('post.');
            //dump($param);die;
            $goods_cat = new ResultModel();
            $flag = $goods_cat->insertData($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $option = model('OptionResultModel')->order('sort asc')->field('id,name')->select();
        $this->assign('option',$option);

        //$this->assign('result_id', $key);
        return $this->fetch();
    }


    /**
     * @return [type] [description]
     * @author felix
     */
    public function resultEdit()
    {
        $goods_cat = new ResultModel();
        if(request()->isAjax()){

            $param = input('post.');
            $flag = $goods_cat->updateData($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $id = input('param.id');
        //$key = input('result_id');
        //$this->assign('result_id', $key);
        $this->assign('photo',$goods_cat->getOneData($id));

        $option = model('OptionResultModel')->order('sort asc')->field('id,name')->select();
        $this->assign('option',$option);

        return $this->fetch();
    }


    /**
     * @return [type] [description]
     * @author felix
     */
    public function resultDel()
    {
        $id = input('param.id');
        $goods_cat = new ResultModel();
        $flag = $goods_cat->delData($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }



    /**
     * @return [type] [description]
     * @author felix
     */
    public function result_state()
    {
        $id = input('param.id');
        $status = Db::name('result')->where('id',$id)->value('is_show');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('result')->where('id',$id)->setField(['is_show'=>-1]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已隐藏']);
        }
        else
        {
            $flag = Db::name('result')->where('id',$id)->setField(['is_show'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已显示']);
        }

    }


}